package top.ffhome.concurrency.example.atomic;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import top.ffhome.common.annotation.ThreadSafe;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

@Slf4j
@ThreadSafe
public class AtomicExample5 {
    private static AtomicIntegerFieldUpdater<AtomicExample5> updater = AtomicIntegerFieldUpdater.newUpdater(AtomicExample5.class, "count");
    @Getter
    private volatile int count = 100;
    private static AtomicExample5 example = new AtomicExample5();

    public static void main(String[] args) {
        if (updater.compareAndSet(example, 100, 120)) {
            log.info("update success 1, {}", example.getCount());
        }
        if (updater.compareAndSet(example, 100, 120)) {
            log.info("update success 2, {}", example.getCount());
        } else {
            log.info("update failed, {}", example.getCount());
        }
    }
}
